package com.hjimi.color;

import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;

import com.hjimi.api.iminect.ImiDevice;
import com.hjimi.api.iminect.ImiFrameMode;
import com.hjimi.api.iminect.ImiFrameType;
import com.hjimi.api.iminect.ImiImageFrame;
import com.hjimi.api.iminect.Utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;


public class SimpleViewer extends Thread {

    private boolean mShouldRun = false;

    private ImiFrameType mFrameType;
    private GLPanel      mGLPanel;
    private DecodePanel  mDecodePanel;
    private ImiDevice    mDevice;
    private ImiFrameMode mCurrentMode;

    private ShowFrameListenners mShowFrameListenners;

    public SimpleViewer(ImiDevice device, ImiFrameType frameType, ShowFrameListenners mShowFrameListenners) {
        this.mShowFrameListenners = mShowFrameListenners;
        mDevice = device;
        mFrameType = frameType;

        mCurrentMode = mDevice.getCurrentFrameMode(ImiDevice.ImiStreamType.COLOR);
    }

    public void setGLPanel(GLPanel GLPanel) {
        this.mGLPanel = GLPanel;
    }

    public void setDecodePanel(DecodePanel decodePanel) {
        this.mDecodePanel = decodePanel;
    }

    @Override
    public void run() {
        super.run();

        //start read frame.
        while (mShouldRun) {
            ImiDevice.ImiFrame nextFrame = mDevice.readNextFrame(ImiDevice.ImiStreamType.COLOR, 25);
            //frame maybe null, if null, continue.
            if (nextFrame == null) {
                continue;
            }

            drawColor(nextFrame);
        }
    }

    /**
     * File f;
     * int c = 0;c++;
     * if(c==300 && f == null){
     * f = new File("/sdcard/test.jpg");
     * try {
     * f.createNewFile();
     * } catch (IOException e) {
     * e.printStackTrace();
     * }
     * byte[] data = new byte[460800];
     * frameDataZD.get(data);
     * YuvImage yuv = new YuvImage(data, ImageFormat.NV21, 640, 480, null);
     * FileOutputStream out = null;
     * try {
     * out = new FileOutputStream(f);
     * } catch (FileNotFoundException e) {
     * e.printStackTrace();
     * }
     * yuv.compressToJpeg(new Rect(0, 0, 640, 480), 100, out);//相片质量
     * }
     */

    private void drawColor(ImiDevice.ImiFrame nextFrame) {


        ByteBuffer frameData = nextFrame.getData();

        if (mShowFrameListenners != null) {
            mShowFrameListenners.showFrame(frameData,nextFrame);
        }
        int width = nextFrame.getWidth();
        int height = nextFrame.getHeight();
        LogPlus.d("------> nextFrameZD  width:" + width + "    height:" + height);
        LogPlus.d("------> nextFrameZD mCurrentMode.getFormat():" + mCurrentMode.getFormat());
        //draw color image.
        switch (mCurrentMode.getFormat()) {
            case IMI_PIXEL_FORMAT_IMAGE_H264:
                if (mDecodePanel != null) {
                    mDecodePanel.paint(frameData, nextFrame.getTimeStamp());
                }
                break;
            case IMI_PIXEL_FORMAT_IMAGE_YUV420SP:
                frameData = Utils.yuv420sp2RGB(nextFrame);
                if (mGLPanel != null) {
                    mGLPanel.paint(null, frameData, width, height);
                }
                break;
            case IMI_PIXEL_FORMAT_IMAGE_RGB24:
                if (mGLPanel != null) {
                    mGLPanel.paint(null, frameData, width, height);
                }
                break;
            default:
                break;
        }
    }

    public void onPause() {
        if (mGLPanel != null) {
            mGLPanel.onPause();
        }
    }

    public void onResume() {
        if (mGLPanel != null) {
            mGLPanel.onResume();
        }
    }

    public void onStart() {
        if (!mShouldRun) {
            mShouldRun = true;

            //start read thread
            this.start();
        }
    }

    public void onDestroy() {
        mShouldRun = false;
    }

    public interface ShowFrameListenners {
        void showFrame(ByteBuffer frameData,ImiDevice.ImiFrame nextFrame);
    }
}
